<template>
  <j-modal
    :title="title"
    :width="840"
    :visible="visible"
    :maskClosable="false"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    :fullscreen="false"
    @close="close"
    :destroyOnClose="true"
    cancelText="关闭"
  >
    <a-form-model ref="ruleForm" :model="submitData" :rules="rules">
      <!-- TODO 单位表格 -->
      <div style="margin: 0 auto" :class="{ isCheck: isCheck }" v-if="formType == '单位'">
        <table class="table table-striped table-bordered" align="center" valign="center">
          <tr>
            <td class="column">单位全称</td>
            <td class="value" colspan="3">
              <a-input v-model="submitData.unitName" disabled></a-input>
            </td>
            <!--          无-->
            <td class="column">申报单位负责人</td>
            <td class="value">
              <a-input v-model="submitData.unitHead" :disabled="disableSubmit"></a-input>
            </td>
          </tr>
          <tr>
            <td class="column item-required" rowspan="2">申报单位人数</td>
            <td class="value" rowspan="2">
              <a-form-model-item prop="unitTotalNum">
                <a-input-number v-model="submitData.unitTotalNum" :min="0" :disabled="disableSubmit"></a-input-number>
              </a-form-model-item>
            </td>
            <td class="column">男</td>
            <td class="value">
              <a-form-model-item prop="manNum">
                <a-input-number :min="0" v-model="submitData.manNum" :disabled="disableSubmit"></a-input-number>
              </a-form-model-item>
            </td>

            <td class="column">高级职称人数</td>
            <td class="value">
              <a-form-model-item prop="highTitle">
                <a-input-number :min="0" v-model="submitData.highTitle" :disabled="disableSubmit"></a-input-number>
              </a-form-model-item>
            </td>
            <!-- <td class="column">计划负责人</td> -->
          </tr>
          <tr>
            <td class="column">女</td>
            <td class="value">
              <a-form-model-item prop="womanNum">
                <a-input-number :min="0" v-model="submitData.womanNum" :disabled="disableSubmit"></a-input-number>
              </a-form-model-item>
            </td>

            <td class="column">中级职称人数</td>
            <td class="value">
              <a-form-model-item prop="middleTitle">
                <a-input-number :min="0" v-model="submitData.middleTitle" :disabled="disableSubmit"></a-input-number>
              </a-form-model-item>
            </td>
          </tr>
          <tr>
            <td class="column title" colspan="6" style="text-align: center">先进事迹摘要</td>
            <!-- <td class="column title" colspan="6">先进事迹摘要</td> -->
          </tr>
          <tr>
            <td class="value" colspan="12">
              <a-textarea
                :placeholder="disableSubmit ? '' : '先进材料应在1500字以内'"
                v-model="submitData.storyAbstract"
                :max-length="1500"
                :disabled="disableSubmit"
                :auto-size="{ minRows: 20, maxRows: 22 }"
              ></a-textarea>
            </td>
          </tr>
        </table>
      </div>

      <!-- TODO 个人表格 -->
      <div style="margin: 0 auto" :class="{ isCheck: isCheck }" v-if="formType == '个人'">
        <table class="table table-striped table-bordered" align="center" valign="center">
          <tr>
            <td class="column item-required">姓名</td>
            <td class="value">
              <a-form-model-item prop="peoName">
                <a-input v-model="submitData.peoName" :disabled="disableSubmit"></a-input>
              </a-form-model-item>
            </td>
            <!--          无-->
            <td class="column item-required">性别</td>
            <td class="value">
              <a-form-model-item prop="peoSex">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="submitData.peoSex"
                  dict-code="sex1"
                  :disabled="disableSubmit"
                />
              </a-form-model-item>

              <!-- <a-select v-model="submitData.peoSex" style="width: 100%">
                <a-select-option value="男">男</a-select-option>
                <a-select-option value="女">女</a-select-option>
              </a-select> -->
              <!-- <a-input v-model="submitData.peoSex" :disabled="disableSubmit"></a-input> -->
            </td>
            <td class="column item-required">年龄</td>
            <td class="value">
              <a-form-model-item prop="peoAge">
                <a-input-number :min="1" v-model="submitData.peoAge" :disabled="disableSubmit"></a-input-number>
              </a-form-model-item>
            </td>
          </tr>
          <tr>
            <td class="column item-required">政治面貌</td>
            <td class="value">
              <a-form-model-item prop="politicsStatus">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="submitData.politicsStatus"
                  dict-code="political_status"
                  :disabled="disableSubmit"
                />
              </a-form-model-item>

              <!-- <a-input v-model="submitData.politicsStatus" :disabled="disableSubmit"></a-input> -->
            </td>
            <!--          无-->
            <td class="column item-required">职务</td>
            <td class="value">
              <a-form-model-item prop="post">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="submitData.post"
                  dict-code="job_structure"
                  :disabled="disableSubmit"
                />
              </a-form-model-item>

              <!-- <a-input v-model="submitData.post" :disabled="disableSubmit"></a-input> -->
            </td>
            <td class="column item-required">职称</td>
            <td class="value">
              <!-- TODO 职称字典 -->
              <j-dict-select-tag
                style="width: 100%"
                v-model="submitData.title"
                dict-code="xj_zc"
                :disabled="disableSubmit"
              />
              <!-- <a-input v-model="submitData.title" :disabled="disableSubmit"></a-input> -->
            </td>
          </tr>
          <tr>
            <td class="column">执业资格</td>
            <td class="value" colspan="3">
              <a-input v-model="submitData.skillRight" :disabled="disableSubmit"></a-input>
            </td>

            <td class="column item-required">学历</td>
            <td class="value">
              <a-form-model-item prop="educat">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="submitData.educat"
                  dict-code="knowledge_structure"
                  :disabled="disableSubmit"
                />
              </a-form-model-item>

              <!-- <a-input v-model="submitData.educat" :disabled="disableSubmit"></a-input> -->
            </td>
          </tr>
          <tr>
            <td class="column title" colspan="6" style="text-align: center">先进事迹摘要</td>
            <!-- <td class="column title" colspan="6">先进事迹摘要</td> -->
          </tr>
          <tr>
            <td class="value" colspan="12">
              <a-textarea
                :placeholder="disableSubmit ? '' : '先进材料应在1500字以内'"
                v-model="submitData.storyAbstract"
                :max-length="1500"
                :auto-size="{ minRows: 20, maxRows: 22 }"
                :disabled="disableSubmit"
              ></a-textarea>
            </td>
          </tr>
        </table>
      </div>
    </a-form-model>

    <template slot="footer">
      <a-button :disabled="disableSubmit" style="padding: 0 10px" type="primary" @click="saveData(0)">保存</a-button>
      <a-button :disabled="disableSubmit" style="padding: 0 10px" type="primary" @click="saveData(1)">提交</a-button>
    </template>
  </j-modal>
</template>
<script>
import { getAction, postAction } from '@/api/manage'
import columns from './columns'

export default {
  name: 'SysPositionModal',
  components: {},
  data() {
    return {
      columns: columns(this),
      dataSource: [],
      ipagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        onChange: (pageNumber) => {
          this.onChange(pageNumber)
        },
        showQuickJumper: true,
        total: 0,
      },
      disableSubmit: false,
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      url: {
        // get: '/sjzyzhgl/sjPlanInfo/planInfoList',
        // exportList: '/sjzyzhgl/sjPlanInfo/exportXlsByTemplate',
        list: '/check/assessment/selectByNo',
        save: '/check/assessment/editAdvanceDept',
        savePeo: '/check/assessment/editAdvancePeo',
      },
      formType: '',
      organShow: true,
      queryParam: {
        unitname: '',
        unit: '',
      },
      rules: {
        // unitCode: [{ required: true, message: '请选择单位' }],
        unitTotalNum: [{ required: true }, { pattern: /^[+]{0,1}(\d+)$/ }],
        peoAge: [{ required: true }, { pattern: /^[+]{0,1}(\d+)$/ }],
        peoName: [{ required: true }],
        manNum: [{ pattern: /^[+]{0,1}(\d+)$/ }],
        highTitle: [{ pattern: /^[+]{0,1}(\d+)$/ }],
        womanNum: [{ pattern: /^[+]{0,1}(\d+)$/ }],
        middleTitle: [{ pattern: /^[+]{0,1}(\d+)$/ }],
        peoSex: [{ required: true }],
        politicsStatus: [{ required: true }],
        post: [{ required: true }],
        educat: [{ required: true }],
      },
      rowData: {},
      submitData: {
        unitName:'',
        unitHead:'',
        unitTotalNum:'',
        manNum:'',
        highTitle:'',
        womanNum:'',
        middleTitle:'',
        storyAbstract:'',
        peoName:'',
        peoSex:'',
        peoAge:'',
        politicsStatus:'',
        post:'',
        title:'',
        skillRight:'',
        educat:'',
        storyAbstract:'',
      },
    }
  },
  created() {},
  methods: {
    // check(record) {
    //   this.model = Object.assign({}, record)
    //   this.visible = true
    //   debugger
    //   this.httpRequest()
    //   this.list()
    // },
    edit(record) {
      // debugger
      this.formType = record.advanceType
      // console.log(record, 'edit')
      if (record.advanceType == '单位') {
        this.submitData.unitName = record.submitDeptName
      }
      this.rowData = record
      this.getTableData(record.id, record.advanceType)
      // this.visible = true
    },
    async getTableData(id, type) {
      const { result } = await getAction(`${this.url.list}?id=${id}&advanceType=${type}`)
      if(result === null) {
         // 说明这是新增项的编辑，返回为空
         this.submitData = this.$options.data().submitData; // 重置
      }else{
          this.submitData = Object.assign({}, this.submitData, result)
          debugger
      }
      this.visible = true
    },
    saveData(type) {
      let params = {
        advanceId: this.rowData.id,
        submitFlag: type,
        ...this.submitData,
      }
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          postAction(this.formType == '单位' ? this.url.save : this.url.savePeo, params).then((res) => {
            if (res.success) {
              this.$message.success(res.message)
              this.visible = false
              this.submitData = {}
              this.$emit('submitd')
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          this.$message.error('请确认所填项是否正确!!!')
          return false
        }
      })
    },
    list() {
      getAction(this.url.list, {
        proId: this.model.id,
        pageSize: this.ipagination.pageSize,
        pageNo: this.ipagination.current,
      }).then((res) => {
        if (res.success && res.result) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        } else {
          this.ipagination.total = 0
        }
      })
    },
    onChange(pageNumber) {
      this.ipagination.current = pageNumber
      this.list()
    },
    exportList() {
      getAction(this.url.exportList, { id: this.model.id })
        .then((res) => {
          if (res.success && res.result) {
            this.$message.success('正在下载，请稍等！')
            let url = `${window._CONFIG['domianURL']}/sys/common/static${res.result}`
            window.open(url, '_blank')
          } else {
          }
        })
        .finally(() => {})
    },
    close(isSubmit) {
      if (isSubmit) {
      } else {
        this.visible = false
      }
    },
  },
}
</script>

<style lang="less" scoped>
@import '../../css/checkAndEdit.less';
</style>
